﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Login : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userType"] != null && Session["userName"] != null)
        {
            switch (Session["userType"].ToString())
            {
                case "SinhVien":
                    Response.Redirect("~/Student.aspx");
                    break;
                case "GiangVien":
                    Response.Redirect("~/Professor.aspx");
                    break;
                case "PhongDaoTao":
                    Response.Redirect("~/Principal.aspx");
                    break;
                case "Admin":
                    Response.Redirect("~/Admin.aspx");
                    break;
            }
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        SqlConnection conn = null;
        SqlDataAdapter dataAdapter = null;
        DataTable dataTable = null;
        bool loginSuccess = false;

        try
        {
            conn = new SqlConnection(connectionString);
            string userType = "";

            switch(radioUserType.SelectedIndex)
            {
                case 0:
                    dataAdapter = new SqlDataAdapter("Select Password FROM SinhVien where MSSV ='" + Username.Text + "'", conn);
                    userType = "SinhVien";
                    break;
                case 1:
                    dataAdapter = new SqlDataAdapter("Select Password FROM GiangVien where MSGV ='" + Username.Text + "'", conn);
                    userType = "GiangVien";
                    break;
                case 2:
                    dataAdapter = new SqlDataAdapter("Select Password FROM PhongDaoTao where Username ='" + Username.Text + "'", conn);
                    userType = "PhongDaoTao";
                    break;
                case 3:
                    dataAdapter = new SqlDataAdapter("Select Password FROM Admin where Username ='" + Username.Text + "'", conn);
                    userType = "Admin";
                    break;
            }

            dataTable = new DataTable();
            dataAdapter.Fill(dataTable);

            if (dataTable.Rows.Count > 0 && dataTable.Rows[0].ItemArray != null && (String)dataTable.Rows[0][0] == Password.Text)
            {
                Session["userName"] = Username.Text;
                Session["userType"] = userType;

                loginSuccess = true;
            }
            else 		
            { 		
                ShowMessage("Sai tên đăng nhập hoặc mật khẩu !", ""); 		
            }

            dataTable.Dispose();
            dataTable = null;
            dataAdapter.Dispose();
            dataAdapter = null;
            conn.Close();
        }
        catch(SqlException)
        {
            ShowMessage("Lỗi đăng nhập !", "");
        }

        if(loginSuccess)
        {
            switch(radioUserType.SelectedIndex)
            {
                case 0:
                    Response.Redirect("~/Student.aspx");
                    break;
                case 1:
                    Response.Redirect("~/Professor.aspx");
                    break;
                case 2:
                    Response.Redirect("~/Principal.aspx");
                    break;
                case 3:
                    Response.Redirect("~/Admin.aspx");
                    break;
            }
        }
    }
}